package com.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
@Component
public class IDUtils {
    private static final long BEGIN_TIMESTAMP = 1640995200L;

    @Autowired
    private  RedisTemplate redisTemplate;

    public  long getId(){
        LocalDateTime now = LocalDateTime.now();

        long nowstamp = now.toEpochSecond(ZoneOffset.UTC);
        long timestamp = nowstamp-BEGIN_TIMESTAMP;
        //获取当前日期
        String Date = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //获取自增增长值
        Long increment = redisTemplate.opsForValue().increment("video:" + Date);
        long id = timestamp<<32|increment;
        return id;
    }
}
